package cc.rengu.igas.bomp.biz.service.impl;

import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.facade.BompTransMonitorService;
import cc.rengu.igas.bomp.facade.request.KeySynchroniseRequest;
import cc.rengu.igas.bomp.facade.request.TransMonitorRequest;
import cc.rengu.igas.bomp.facade.response.KeySynchroniseResponse;
import cc.rengu.igas.bomp.facade.response.TransMonitorResponse;
import cc.rengu.igas.bomp.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class BompTransMonitorServiceImpl implements BompTransMonitorService {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<TransMonitorResponse> subscribeTransMonitorInfo(TransMonitorRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<TransMonitorResponse> result = new Result<>();
        TransMonitorResponse response = new TransMonitorResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, TransMonitorResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((TransMonitorResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("交易监控处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }

    @Override
    public Result<KeySynchroniseResponse> keySynchronise(KeySynchroniseRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<KeySynchroniseResponse> result = new Result<>();
        KeySynchroniseResponse response = new KeySynchroniseResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, KeySynchroniseResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((KeySynchroniseResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("交易监控处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                response.setRespCode(((BizException) e).getCode());
                response.setRespDesc(e.getMessage());
            } else {
                response.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                response.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
            result.setResult(response);
        }
        return result;
    }
}
